//
//  ExperienceVoucherManagerTableViewController.m
//  GoodWayClient
//
//  Created by HuiDong on 15/12/3.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "ExperienceVoucherManagerTableViewController.h"
#import "ExperienceVoucherTableViewCell.h"
#import "ShowMore.h"
#import "ExperienceVoucherMoreTableViewController.h"
#import "Constant.h"
#import "AFNetworking.h"
#import "AppDelegate.h"
#import "ExperienceVoucher.h"
#import "ExperienceVoucherList.h"
#import "CardInfo.h"

@interface ExperienceVoucherManagerTableViewController ()

@property (weak, nonatomic) IBOutlet UILabel *Used;
@property (weak, nonatomic) IBOutlet UILabel *left;
@property (nonatomic, strong)NSMutableArray *array1;
@property (nonatomic, assign) int a;

@end

@implementation ExperienceVoucherManagerTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //页面的显示数据
    //网络请求
    AFHTTPRequestOperationManager *request = [AFHTTPRequestOperationManager manager];
    request.responseSerializer = [AFJSONResponseSerializer serializer];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    dic[@"token"] = tempAppDelegate.token;
    
    [request POST:[NSString stringWithFormat:@"%@%@",url2,@"GetMyCardMsg"] parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if ([[NSString stringWithFormat:@"%@",dic[@"Status"]] isEqualToString:@"0"]) {
            NSDictionary *dic1 = dic[@"Data"];
            ExperienceVoucher *experienceVoucher = [[ExperienceVoucher alloc] initWithDic:dic1];
            self.Used.text = [NSString stringWithFormat:@"%d",experienceVoucher.UsedNum];
            self.left.text = [NSString stringWithFormat:@"%d",(experienceVoucher.MaxNum-experienceVoucher.UsedNum)];
        }else
        {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:dic[@"Message"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"系统网络申请错误!");
    }];

    
    
    //网络请求
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
    dic2[@"token"] = tempAppDelegate.token;
    dic2[@"pageSize"] = @10;
    dic2[@"pageIndex"] = [NSNumber numberWithInt:(++self.a)];
    dic2[@"status"] = @0;
    
    [request POST:[NSString stringWithFormat:@"%@%@",url2,@"BluePeoGetCardList"] parameters:dic2 success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if ([[NSString stringWithFormat:@"%@",dic[@"Status"]] isEqualToString:@"0"]) {
            NSDictionary *dic1 = dic[@"Data"];
            NSArray *array = dic1[@"list"];
            self.array1 = [NSMutableArray arrayWithArray:array];
            [self.tableView reloadData];
        }else
        {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:dic[@"Message"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"系统网络申请错误!");
    }];

    
    //footView的加载更多的显示
    ShowMore *showMore = [ShowMore footView];
    
    __weak ExperienceVoucherManagerTableViewController *tempController = self;
    
    ShowMoreBlock block = ^{
        //网络请求
        [request POST:[NSString stringWithFormat:@"%@%@",url2,@"BluePeoGetCardList"] parameters:dic2 success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = (NSDictionary *)responseObject;
            if ([[NSString stringWithFormat:@"%@",dic[@"Status"]] isEqualToString:@"0"]) {
                NSDictionary *dic1 = dic[@"Data"];
                NSArray *array = dic1[@"list"];
                [tempController.array1 addObjectsFromArray:array];
                [tempController.tableView reloadData];;
            }else
            {
                [[[UIAlertView alloc] initWithTitle:@"提示" message:dic[@"Message"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"系统网络申请错误!");
        }];
    };
    
    showMore.block = block;
    self.tableView.tableFooterView = showMore;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//懒加载

-(NSMutableArray *)array1
{
    if (!_array1) {
        _array1 = [NSMutableArray array];
    }
    return _array1;
}

//数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array1.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ExperienceVoucherTableViewCell *cell = [ExperienceVoucherTableViewCell ExperienceVoucherWithTableView:tableView];
    cell.dic = self.array1[indexPath.row];
    return cell;
}

//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

//点击对应的行进行跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ExperienceVoucherMoreTableViewController *more = [storyboard instantiateViewControllerWithIdentifier:@"ExperienceVoucherMoreTableViewController"];
    [self presentViewController:more animated:YES completion:nil];
}

@end
